package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.activity.TeacherDetailsAct;
import com.tutor.lzm.adapter.MyTeacherAdapter;
import com.tutor.lzm.app.Constant;
import com.tutor.lzm.bean.TeacherDetailInfo;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.MyTeacherListRequest;
import com.tutor.lzm.http.response.TeacherDetailListResponse;
import com.tutor.syslib.Logs;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2017/10/10.
 * 我的老师
 */
public class MyTeacherAct extends BaseActivity {
    ListView listView = null;
    private MyTeacherAdapter adapter;
    private List<TeacherDetailInfo> teacherList = new ArrayList<>();
    /**
     * 加载位置坐标
     */
    private int currentPage = 0;

    public static void start(Context context) {
        Intent intent = new Intent(context, MyTeacherAct.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_listview);
        initTitle("我的老师");
        initListView();
        getDeliveryTeacherList(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getDeliveryTeacherList(true);
    }

    /**
     * 初始化
     */
    private void initListView() {
        listView = (ListView) findViewById(R.id.listView);
        listView.setHeaderDividersEnabled(false);
        listView.setDividerHeight(0);
        adapter = new MyTeacherAdapter(this, teacherList);
        listView.setAdapter(adapter);
        ViewStub mViewStub = (ViewStub) findViewById(R.id.emptyView);
        listView.setEmptyView(mViewStub);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TeacherDetailInfo bean = teacherList.get(position);
                String teacherId = bean.teacherId;
                TeacherDetailsAct.start(MyTeacherAct.this, teacherId, Constant.teacher_is_select);
            }
        });
    }


    /**
     * 加载老师列表
     *
     * @param isClear
     */
    private void getDeliveryTeacherList(final boolean isClear) {

        if (isClear) {
            currentPage = 0;
        }

        final MyTeacherListRequest request = new MyTeacherListRequest();
//        TeacherListRequest request = new TeacherListRequest();
        request.userId = AppSession.getInstance().getUserId();
        request.start = currentPage;
        request.limit = 20;
//        request.setUserId(AppSession.getInstance().getUserId());
//        request.setStart(currentPage);
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                Logs.e("mainfragment", "response");
                TeacherDetailListResponse teacherListResponse = (TeacherDetailListResponse) response;
                List<TeacherDetailInfo> adResponse = teacherListResponse.teacherList;

                if (adResponse != null && adResponse.size() != 0) {
                    if (isClear) {
                        teacherList.clear();
                    }
                    currentPage++;
                    teacherList.addAll(adResponse);
                    adapter.notifyDataSetChanged();

                }
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);

            }
        });

    }


}
